home *** CD-ROM | disk | FTP | other *** search
/ Atari Mega Archive 1 / Atari Mega Archive - Volume 1.iso / apps / wordproc / 1wp2rtf.zoo / wp2rtf.c < prev    next >
C/C++ Source or Header  |  1992-04-29  |  8KB  |  392 lines

  1. #include    <ctype.h>
  2. #include    <stdio.h>
  3. #include    <stdlib.h>
  4. #include    <string.h>
  5.  
  6. #define    eq(a,b)        !stricmp(a,b)
  7. #define    eqn(a,b,n)    !strnicmp(a,b,n)
  8.  
  9. #define    FALSE    0
  10. #define    TRUE    !FALSE
  11. #define    NEWLINE    "\r\n"
  12.  
  13. #define    NONE    0
  14. #define    BOLD    1
  15. #define    ITALIC    2
  16. #define    UNDERLINED    3
  17. #define    LIGHT    4
  18. #define    SUPER    5
  19. #define    SUB    6
  20. #define    RETURN    8
  21.  
  22. void    usage(void);
  23. int        init_table(char *);
  24. void    translate(char *);
  25.  
  26.  
  27. char    table[256][16];
  28. char    pre[10][256];
  29. char    para[32];
  30. char    post[64];
  31. char    mode[16][10];
  32. char    footnote[256];
  33. char    endnote[16];
  34.  
  35. main(int argc, char *argv[])
  36. {
  37.     int    file;
  38.  
  39.     if(argc <= 1)
  40.     {
  41.         usage();
  42.         return(-1);
  43.     }
  44.     file = 1;
  45.     if(eq(argv[1],"-t"))
  46.     {
  47.         if(argc < 3)
  48.         {
  49.             usage();
  50.             return(-1);
  51.         }
  52.         if(!init_table(argv[2]))
  53.         {
  54.             return(-1);
  55.         }
  56.         file = 3;
  57.     }
  58.     else
  59.     {
  60.         if(!init_table("wp2rtf.x"))
  61.         {
  62.             return(-1);
  63.         }
  64.     }
  65.     for(;file < argc; file++)
  66.     {
  67.         translate(argv[file]);
  68.     }
  69.     return(0);
  70. }
  71.  
  72. void usage(void)
  73. {
  74.     fprintf(stderr, "Usage: wp2rtf [-t translator] file ...\r\n");
  75. }
  76.  
  77. void mycopy(char *dest, char *src)
  78. {
  79.     while(*src && *src != '\r' && *src != '\n')
  80.     {
  81.         *dest++ = *src++;
  82.     }
  83.     *dest = 0;
  84. }
  85.  
  86. int    hex2num(char digit)
  87. {
  88.     digit = toupper(digit);
  89.     if(digit >= '0'  && digit <= '9') return(digit - '0');
  90.     if(digit >= 'A'  && digit <= 'F') return(digit - 'A' + 10);
  91.     return(0);
  92. }
  93.  
  94. int init_table(char *file)
  95. {
  96.     FILE    *in;
  97.     int     i;
  98.     char    buf[1024];
  99.  
  100.     for(i = 0; i<10; i++) pre[i][0] = 0;
  101.     post[0] = para[0] = footnote[0] = endnote[0] = 0;
  102.     for(i = 0; i<16; i++) mode[i][0] = 0;
  103.     for(i = 0; i<256; i++)
  104.     {
  105.         table[i][0] = i;
  106.         table[i][1] = 0;
  107.     }
  108.     table[30][0] = 32;
  109.     in = fopen(file,"rb");
  110.     if(in == NULL)
  111.     {
  112.         fprintf(stderr,"Error opening %s for reading\r\n",file);
  113.         return(FALSE);
  114.     }
  115.     while(fgets(buf,1024,in) != NULL)
  116.     {
  117.         if(buf[0] == '$')
  118.         {
  119.             i = (16*hex2num(buf[1])) + hex2num(buf[2]);
  120.             mycopy(table[i], &buf[4]);
  121.         }
  122.         else
  123.         {
  124.             if(eqn("init",buf,4))
  125.             {
  126.                 strcpy(pre[buf[4]-'0'],&buf[6]);
  127.             }
  128.             if(eqn("exit",buf,4))
  129.             {
  130.                 mycopy(post,&buf[5]);
  131.             }
  132.             if(eqn("paragraph",buf,9))
  133.             {
  134.                 strcpy(para,&buf[10]);
  135.             }
  136.             if(eqn("bold",buf,4))
  137.             {
  138.                 mycopy(mode[BOLD],&buf[5]);
  139.             }
  140.             if(eqn("underlined",buf,10))
  141.             {
  142.                 mycopy(mode[UNDERLINED],&buf[11]);
  143.             }
  144.             if(eqn("italic",buf,6))
  145.             {
  146.                 mycopy(mode[ITALIC],&buf[7]);
  147.             }
  148.             if(eqn("light",buf,5))
  149.             {
  150.                 mycopy(mode[LIGHT],&buf[6]);
  151.             }
  152.             if(eqn("super",buf,5))
  153.             {
  154.                 mycopy(mode[SUPER],&buf[6]);
  155.             }
  156.             if(eqn("sub",buf,3))
  157.             {
  158.                 mycopy(mode[SUB],&buf[4]);
  159.             }
  160.             if(eqn("off",buf,3))
  161.             {
  162.                 mycopy(mode[RETURN],&buf[4]);
  163.             }
  164.             if(eqn("footnote",buf,8))
  165.             {
  166.                 mycopy(footnote,&buf[9]);
  167.             }
  168.             if(eqn("endnote",buf,7))
  169.             {
  170.                 mycopy(endnote,&buf[8]);
  171.             }
  172.         }
  173.     }
  174.     fclose(in);
  175.     return(TRUE);
  176. }
  177.  
  178. void translate(char *filename)
  179. {
  180.     FILE    *in, *out;
  181.     char    outfile[128], *ptr, *buffer, tmp[32];
  182.     long    filesize, current = 0L, i;
  183.     int    linestart, first = TRUE, Mode, fnum;
  184.  
  185.     in = fopen(filename,"rb");
  186.     if(in == NULL)
  187.     {
  188.         fprintf(stderr,"Error opening %s for reading\r\n",filename);
  189.         return;
  190.     }
  191.     fseek(in, 0L, SEEK_END);
  192.     filesize = ftell(in);
  193.     fseek(in, 0L, SEEK_SET);
  194.     buffer = malloc(filesize+1);
  195.     if(buffer == NULL)
  196.     {
  197.         fprintf(stderr,"Insufficient memory to read %s\r\n",filename);
  198.         fclose(in);
  199.         return;
  200.     }
  201.     if(fread(buffer,sizeof(char),filesize,in) != filesize)
  202.     {
  203.         fprintf(stderr,"Error reading %s\r\n",filename);
  204.         fclose(in);
  205.         return;
  206.     }
  207.     fclose(in);
  208.     strcpy(outfile,filename);
  209.     ptr = strrchr(outfile,'.');
  210.     if(ptr != NULL)
  211.     {
  212.         if(eq(ptr,".rtf"))
  213.         {
  214.             fprintf(stderr,"In- and outfiles must be named differently\r\n");
  215.             free(buffer);
  216.             return;
  217.         }
  218.         strcpy(ptr,".rtf");
  219.     }
  220.     else
  221.     {
  222.         strcat(outfile,".rtf");
  223.     }
  224.     out = fopen(outfile,"wb");
  225.     if(out == NULL)
  226.     {
  227.         fprintf(stderr,"Error opening %s for writing\r\n",outfile);
  228.         return;
  229.     }
  230.     buffer[filesize] = 0;
  231.     while(buffer[filesize-1] == '\r' || buffer[filesize-1] == '\n' || buffer[filesize-1] == 'E')
  232.     {
  233.         buffer[--filesize] = 0;
  234.     }
  235.     for(i = 0; i <=9; i++)
  236.     {
  237.         fprintf(out, pre[i]);
  238.     }
  239.     linestart = TRUE;
  240.     while(current < filesize)
  241.     {
  242.         if(linestart && buffer[current] == '\x1F')
  243.         {
  244.             for(;current < filesize && buffer[current] != '\r'; current++);
  245.         }
  246.         while(linestart && current < filesize && buffer[current] == '\x0B' && buffer[current+1] == '\x11')
  247.         {
  248.             current += 2;
  249.         }
  250.         linestart = FALSE;
  251.         switch(buffer[current])
  252.         {
  253.             case        0:
  254.             case    '\r':    if(buffer[current-1] == '\x1e' || buffer[current-1] == '\x19' || buffer[current-1] == '-')
  255.                         {    /*    Soft Break    */
  256.                             fprintf(out,NEWLINE);
  257.                         }
  258.                         else
  259.                         {    /*    Real paragraph break    */
  260.                             fprintf(out,para);
  261.                         }
  262.                         if(buffer[current+1] == '\n') current++;
  263.                         linestart = TRUE;
  264.                         break;
  265.             case    '\n':    linestart = TRUE;
  266.                         break;
  267.             case    '\f':    if(buffer[current+1] == '\x1B' && buffer[current+2] == '\x80')
  268.                         {
  269.                             current += 2;
  270.                         }
  271.                         break;
  272.             case    '\x1B':
  273.                 switch(buffer[++current])
  274.                 {
  275.                     case '\x81':    Mode = BOLD;
  276.                                 break;
  277.                     case '\x88':    Mode = UNDERLINED;
  278.                                 break;
  279.                     case '\x84':    Mode = ITALIC;
  280.                                 break;
  281.                     case '\x82':    Mode = LIGHT;
  282.                                 break;
  283.                     case '\x90':    if(buffer[current+1] == '\x18')
  284.                                 {
  285.                                     while(buffer[current++] != ',');
  286.                                     fnum = atoi(&buffer[current]);
  287.                                     /*    Now find footnote and insert it    */
  288.                                     fprintf(out,footnote);
  289.                                     sprintf(tmp,"\x1FN%03u",fnum);
  290.                                     ptr = strstr(&buffer[current], tmp);
  291.                                     if(ptr != NULL)
  292.                                     {
  293.                                         first = TRUE;
  294.                                         while(*ptr != '\r' && *ptr != '\n')
  295.                                         {
  296.                                             *ptr = '\xFF';
  297.                                             ptr++;
  298.                                         }
  299.                                         while(*ptr == '\r' || *ptr == '\n')
  300.                                         {
  301.                                             *ptr = '\xFF';
  302.                                             ptr++;
  303.                                         }
  304.                                         while(*ptr != '\x1F' && *ptr)
  305.                                         {
  306.                                             switch(*ptr)
  307.                                             {
  308.                                                 case        0:    break;
  309.                                                 case    '\r':    if(buffer[current-1] == '\x1e' || buffer[current-1] == '\x19')
  310.                                                             {    /*    Soft Break    */
  311.                                                                 fprintf(out,NEWLINE);
  312.                                                             }
  313.                                                             else
  314.                                                             {    /*    Real paragraph break    */
  315.                                                                 fprintf(out,para);
  316.                                                             }
  317.                                                             if(buffer[current+1] == '\n') current++;
  318.                                                             linestart = TRUE;
  319.                                                             break;
  320.                                                 case    '\n':    linestart = TRUE;
  321.                                                             break;
  322.                                                 case    '\x1B':
  323.                                                     switch(*++ptr)
  324.                                                     {
  325.                                                         case '\x81':    Mode = BOLD;
  326.                                                                     break;
  327.                                                         case '\x88':    Mode = UNDERLINED;
  328.                                                                     break;
  329.                                                         case '\x84':    Mode = ITALIC;
  330.                                                                     break;
  331.                                                         case '\x82':    Mode = LIGHT;
  332.                                                                     break;
  333.                                                         case '\x90':    Mode = SUPER;
  334.                                                                     break;
  335.                                                         case '\xA0':    Mode = SUB;
  336.                                                                     break;
  337.                                                         case '\x80':    if(first)
  338.                                                                     {
  339.                                                                         Mode = NONE;
  340.                                                                         first = FALSE;
  341.                                                                         break;
  342.                                                                     }
  343.                                                                     Mode = RETURN;
  344.                                                                     break;
  345.                                                         default:        Mode = NONE;
  346.                                                                     break;
  347.                                                     }
  348.                                                     fprintf(out,mode[Mode]);
  349.                                                     break;
  350.                                                 default:    fprintf(out,table[*ptr]);
  351.                                                         break;
  352.                                             }
  353.                                             *ptr++ = '\xFF';
  354.                                         }
  355.                                         if(!strncmp(ptr,"\x1F\E\r\n\x1B\x80",6))
  356.                                         {
  357.                                             strncpy(ptr,"\xFF\xFF\xFF\xFF\xFF\xFF", 6);
  358.                                         }
  359.                                         first = FALSE;
  360.                                     }
  361.                                     fprintf(out,endnote);
  362.                                     while(buffer[current++] != '\x18');
  363.                                     while(buffer[current++] != '\x80');
  364.                                     current--;
  365.                                     Mode = NONE;
  366.                                     break;
  367.                                 }
  368.                                 Mode = SUPER;
  369.                                 break;
  370.                     case '\xA0':    Mode = SUB;
  371.                                 break;
  372.                     case '\x80':    if(first)
  373.                                 {
  374.                                     Mode = NONE;
  375.                                     first = FALSE;
  376.                                     break;
  377.                                 }
  378.                                 Mode = RETURN;
  379.                                 break;
  380.                     default:        Mode = NONE